<?php
namespace app\controllers;

use Yii;
use yii\web\Controller; // 导入Controller
use app\common\ErrorCode;
use udokmeci\yii2beanstalk\BeanstalkController;//加入队列控制器
use GatewayClient\Gateway;
use app\models\GameUser;

/*
集成各类通用函数的控制器
包括返回数据、队列输入....
*/

class BaseController extends Controller
{
    public $enableCsrfValidation = false;
    //绑定userid和client_id
    public function bind($client_id, $uid){
    	Gateway::$registerAddress = '127.0.0.1:1236';
        // client_id与uid绑定
        Gateway::bindUid($client_id, $uid);
    }
    
    //client_id绑定群组
    public function joinGroup($client_id, $gropid){
        Gateway::$registerAddress = '127.0.0.1:1236';
        Gateway::joinGroup($client_id, $gropid);
    }
    
    //根据userid单独发送个人信息
    public function sendMessage($uid, $message){
    	// 设置GatewayWorker服务的Register服务ip和端口，请根据实际情况改成实际值(ip不能是0.0.0.0)
        Gateway::$registerAddress = '127.0.0.1:1236';
        // 向任意uid的网站页面发送数据
        Gateway::sendToUid($uid, $message);
    }

    //根据client_id单独发送个人信息
    public function sendClientId($clientid, $message){
        Gateway::$registerAddress = '127.0.0.1:1236';
        Gateway::sendToClient($clientid,$message);
    }

    //根据group_id发送群组消息
    public function sendGroupId($groupid, $message){
        Gateway::$registerAddress = '127.0.0.1:1236';
        Gateway::sendToGroup($groupid, $message);
    }

    //回复用的代码
    public function SuccessRespone($data){
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        return [
            'code' => ErrorCode::$SUCCESS,
            'code_info' => '成功',
            'data' => $data,
        ];
    }
    public function FailRespone($data){
        if( $data['0'] == 0 ){//502
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return [
                'code' => ErrorCode::$DATABASE_NOT_FOUND,
                'code_info' => '数据库查找不到信息',
                'data' => $data['1'],
            ];
        }else if( $data['0'] == -1 ){//503
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return [
                'code' => ErrorCode::$DATABASE_ERROR,
                'code_info' => '录入数据库失败',
                'data' => $data['1'],
            ];
        }else if( $data['0'] == -2 ){//504
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return [
                'code' => ErrorCode::$RECEIVE_ERROR,
                'code_info' => '接收数据为空',
                'data' => $data['1'],
            ];
        }else if( $data['0'] == -3){//505
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return [
                'code' => ErrorCode::$DATA_ERROR,
                'code_info' => '数据出错',
                'data' => $data['1'],
            ];
        }
    }



    public function RedisExplode($redisinfo){
    	$logininfo = json_decode($redisinfo);
        $logininfo = get_object_vars($logininfo);
    	$openid = $logininfo['openid'];
    	return $openid;
    }

}
